<?php

namespace app\api\controller\homepage;

use app\admin\model\vactivities\Ticket;
use app\common\controller\Api;
use app\common\library\Iden;
use app\admin\model\vactivities\Activity as act;

// 允许跨域
header('Access-Control-Allow-Origin:*');//允许跨域

/**
 * 个人中心
 * 登录用户的个人主页
 * 参与人下订单
 */
class Order extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    private $catemodel = null;
    private $tagsmodel = null;
    private $actmodel = null;
   // private $actpmodel = null;
    private $ordmodel = null;
    private $ticket_model = null;
    protected $aact = null;
    protected $api_act = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\trades\assets\Order;//订单
        $this->actmodel = new \app\admin\model\vactivities\Activity;
        $this->catemodel = new \app\admin\model\superdata\Activitycategory;
        $this->tagsmodel = new \app\admin\model\superdata\Activitytags;
        $this->ticket_model = new \app\admin\model\vactivities\Ticket;
        $this->api_act = new \app\api\controller\activity\Activity;
    }



    /*
     * 个人主页订单活动列表
     */
    public function list(){
        $uInfo = $this->auth->getUserInfo();
        if(empty($uInfo)){//未登录
            $this->error(__('Please login first'), null, 401);
        }
        $admin_id = Iden::decrypt($uInfo['id']);
        if(empty($admin_id)){
            $this->error(__('获取用户参数出错'));
        }
        //$admin_id = 31896;
        $map['status'] = 1;
        //$m['state'] = $this->model::PAY_STATE_2;//已支付
        $per_page = $this->request->get('per_page', 1);
        $limit  = (int)$this->request->get('limit', 20);
        $state = (int)$this->request->get('state',0);//订单状态
        $offset = ($per_page - 1) * $limit;
        $m['buyer_id'] = $admin_id;
        if(!empty($state)){
            $sArr = [$this->model::PAY_STATE_1,$this->modle::PAY_STATE_2,$this->model::PAY_STATE_4];
            if(!in_array($state,$sArr)){
                $this->error('订单状态参数错误','',0);
            }
            $m['state'] = $state;
        }
        $list = $this->model
            ->with(['activity'])
            ->where($map)
            ->where($m)
            ->order('startTime ASC')
            ->limit($offset, $limit);

        $r = $list->select();
        $count = count($r);
        $list = [];

        foreach($r as $k=>$row){
            $row->visible(['id','orderid','account_id','state','name','thumb','amount','paytime','createTime','startTime','duration','summary','category','tags',
                'price','nickname','avatar','link_url','price_min','price_max','series_id','isseries','iscollect']);
            $row->getRelation('activity')->visible(['id','name','thumb','createTime','startTime','isticket','duration','series_id','link_url']);

            $tlist = $this->ticket_model->getTicketList($row['activity']['id'],Ticket::TICKET_TYPE_2,$row['account_id']);
            if($tlist){
                $price = self::max_min($tlist);
            }
            $list[$k]['id'] = Iden::encrypt($row['activity']['id']);
            $list[$k]['orderid'] = $row['orderid'];
            $list[$k]['state'] = $this->model::PayState[$row['state']];
            $list[$k]['paytime'] = !empty($row['paytime']) ? date('Y-m-d H:i:s',$row['paytime']):'';
            $list[$k]['name'] = $row['activity']['name'];
            $list[$k]['price_min'] = isset($price) && !empty($price['min']) ? $price['min']:0;
            $list[$k]['price_max'] = isset($price) && !empty($price['max']) ? $price['max']:0;
            $list[$k]['amount'] = pricefentoyuan($row['amount']);
            $list[$k]['summary'] = $row['activity']['summary'];
            $list[$k]['duration'] = s_to_hs($row['activity']['duration']);
            $list[$k]['startTime'] = date('Y-m-d H:i:s',$row['activity']['startTime']);
            $list[$k]['createTime'] = date('Y-m-d H:i:s',$row['createTime']);
            $list[$k]['isseries'] = isset($row['activity']['series_id']) && ($row['activity']['series_id'] > 0) ? 1:0;
            $list[$k]['iscollect'] = $this->api_act->Iscollect($row['activity']['id']);//是否已收藏

            if($row['activity']['thumb']){
                $thumbArr = explode(',',$row['activity']['thumb']);
                $list[$k]['thumb'] = !empty($thumbArr) ? $thumbArr:'';
            }

        }

        $result = array("total" =>$count, "rows" => $list);

        $this->success(__('读取成功'), $result, 200);
    }

    /*
    * $tlist 获取的价格数组
    * 最大值与最小值
    */
    static public function max_min($tlist){
        if(empty($tlist))
            return false;
        $m = [];
        foreach($tlist as $v){
            $m[] = $v['price'];
        }

        $max = max($m);
        $min = min($m);
        unset($m);
        $data['max'] = $max;
        $data['min'] = $min;
        //$price = $min.'--'.$max;
        return $data;
    }

}
